package com.ruoyi.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.business.domain.TRegisterNeed;
import com.ruoyi.business.service.ITRegisterNeedService;
import com.ruoyi.business.vo.TRegisterNeedVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TRegisterDconfigVo;
import com.ruoyi.business.service.ITRegisterDconfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 招生月份配置Controller
 *
 * @author Tellsea
 * @date 2025-07-01
 */
@Api(tags = "招生月份配置Controller")
@RestController
@RequestMapping("/business/registerDconfig")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TRegisterDconfigController extends BaseController {

    private final ITRegisterDconfigService tRegisterDconfigService;

    private final ITRegisterNeedService tRegisterNeedService;

    @ApiOperation("查询招生月份配置列表")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:list')")
    @GetMapping("/list")
    public TableDataInfo<TRegisterDconfigVo> list(TRegisterDconfigVo entity) {
        return tRegisterDconfigService.queryList(entity);
    }

    @ApiOperation("查询招生月份配置所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TRegisterDconfigVo entity) {
        return AjaxResult.success("查询成功", tRegisterDconfigService.queryAll(entity));
    }

    @ApiOperation("导出招生月份配置列表")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:export')")
    @Log(title = "招生月份配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRegisterDconfigVo entity) {
        List<TRegisterDconfigVo> list = tRegisterDconfigService.queryAll(entity);
        ExcelUtil<TRegisterDconfigVo> util = new ExcelUtil<>(TRegisterDconfigVo.class);
        util.exportExcel(response, list, "招生月份配置数据");
    }

    @ApiOperation("获取招生月份配置详细信息")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tRegisterDconfigService.queryById(id));
    }

    @ApiOperation("新增招生月份配置")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:add')")
    @Log(title = "招生月份配置", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TRegisterDconfigVo entity) {
        return toAjax(tRegisterDconfigService.save(entity));
    }

    @ApiOperation("修改招生月份配置")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:edit')")
    @Log(title = "招生月份配置", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TRegisterDconfigVo entity) {
        boolean updated = tRegisterDconfigService.updateById(entity);
        // 数据修改完同步招生信息配置数据
        TRegisterNeedVo needVo = new TRegisterNeedVo();
        needVo.setBatchNameList(entity.getRegisterMonth());
        List<TRegisterNeedVo> tRegisterNeedVos = tRegisterNeedService.queryAllOri(needVo);
        for (TRegisterNeedVo registerNeedVo : tRegisterNeedVos) {
            String batchNameList = registerNeedVo.getBatchNameList();
            String batchDateList = registerNeedVo.getBatchDateList();
            String[] batchNameArr = batchNameList.split(",");
            String[] batchDateArr = batchDateList.split(",");
            for (int i = 0; i < batchNameArr.length; i++) {
                if(StringUtils.equals(entity.getRegisterMonth(),batchNameArr[i])){
                    String batchDateStr = entity.getStartDate()+" 00:00:00"+"-"+entity.getEndDate()+" 23:59:59";
                    batchDateArr[i]=batchDateStr;
                }
            }
            registerNeedVo.setBatchDateList(StringUtils.join(batchDateArr,","));
        }
        List<TRegisterNeed> batchParams = BeanUtil.copyToList(tRegisterNeedVos, TRegisterNeed.class);
        tRegisterNeedService.updateBatchById(batchParams);
        return toAjax(updated);
    }

    @ApiOperation("删除招生月份配置")
    @PreAuthorize("@ss.hasPermi('business:registerDconfig:remove')")
    @Log(title = "招生月份配置", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tRegisterDconfigService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
